//
//  ZhiViewController.m
//  網易
//
//  Created by 赵天宇 on 16/8/22.
//  Copyright © 2016年 赵天宇. All rights reserved.
//

#import "ZhiViewController.h"
#import "ASIHTTPRequest.h"
#import <SDCycleScrollView.h>
@interface ZhiViewController ()<UITableViewDataSource,UITableViewDelegate,SDCycleScrollViewDelegate>{
    UITableView *tableV;
    NSDictionary *dic;
}

@end
#define TEXT_URL @"http://data.live.126.net/livechannel/previewlist.json"
@implementation ZhiViewController

- (void)viewDidLoad {
    [super viewDidLoad];  
    //    标题
    self.navigationItem.title=@"網易热门";
    //    改变导航条标题字体大小
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:24]}];
    self.navigationController.navigationBar.barStyle=UIBarStyleBlack;
    self.navigationController.navigationBar.barTintColor=[UIColor redColor];
    tableV = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    tableV.delegate=self;
    tableV.dataSource=self;
    [self.view addSubview:tableV];
    NSURL *url = [NSURL URLWithString:TEXT_URL];
    ASIHTTPRequest *request = [[ASIHTTPRequest alloc]initWithURL:url];
    ASIHTTPRequest *newRequest = [[ASIHTTPRequest alloc]init];
    newRequest=request;
    [request setCompletionBlock:^{
        NSData *data=newRequest.responseData;
        dic =[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        [tableV reloadData];
    }];
    [request startAsynchronous];
    // Do any additional setup after loading the view.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectio{
    NSArray *arr = [dic objectForKey:@"live_review"];
    return arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *ss=@"love";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ss];
    for (UIView *vv in cell.contentView.subviews) {
        [vv removeFromSuperview];
    }
    if(!cell){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ss];
    }
    NSArray *arr =[dic objectForKey:@"live_review"];
    NSDictionary *dic_2 = arr[indexPath.row];
    NSString *imageSource= [dic_2 objectForKey:@"image"];
    NSURL *url = [NSURL URLWithString:imageSource];
    NSData *data = [NSData dataWithContentsOfURL:url];
    UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 180)];
    imageV.image =[UIImage imageWithData:data];
    [cell.contentView addSubview:imageV];
    UILabel *lableTitle = [[UILabel alloc]initWithFrame:CGRectMake(10, 100, 300, 20)];
    lableTitle.text=[dic_2 objectForKey:@"roomName"];
    lableTitle.textColor=[UIColor whiteColor];
    [cell.contentView addSubview:lableTitle];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 180;
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    NSArray *imageArray  = @[@"http://img1.zzkcdn.com/zzk_135401.jpg-roompic.jpg",
                             @"http://img1.zzkcdn.com/i7faa1a1cd50382cf1a08e98b2c131eczzkcopr/2000x1500.jpg-roompic.jpg",
                             @"http://img1.zzkcdn.com/bia25741d700d25f890da05a6474ff7dzzkcopr/2000x1500.jpg-roompic.jpg",
                             @"http://img1.zzkcdn.com/hza9aab0a6369d4063d234954a54df18/2000x1500.jpg-roompic.jpg"];
    SDCycleScrollView *cycleView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200) delegate:self placeholderImage:[UIImage imageNamed:@""]];
    cycleView.imageURLStringsGroup =imageArray;
    return cycleView;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
